using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ShopForWeb.Dtos.ViewModel;
using ShopForWeb.Services;

namespace ShopForWeb.Api.Controllers.User
{
  [ApiController]
  [Route("api/user/[controller]")]

  public class IdentityController : ControllerBase
  {
    private UserServices _userService;
    public IdentityController(UserServices userServices)
    {
      this._userService = userServices;

    }

    [HttpPost("login")]
    public async Task<ResponseView> GetLoginAsync([FromForm] string phone, [FromForm] string password)
    {
      var obj = await _userService.GetUserByLoginAsync(phone, password);
      if (obj == null)
      {
        return new ResponseView()
        {
          Code = 500,
          Data = new { }
        };
      }
      else
      {
        //回执
        return new ResponseView()
        {
          Code = 200,
          Data = new { User = obj }
        };
      }
    }
    [HttpPost("registor")]
    public async Task<ResponseView> AddNewAsync([FromBody] ShopForWeb.Models.Basic.User user)
    {
      int result = await _userService.AddNewAsync(user);
      if (result == 0)
        return new ResponseView
        {
          Code = 500,
          Message = "用户数据添加失败"
        };
      else
        return new ResponseView
        {
          Code = 200,
          Message = "用户数据添加成功"
        };
    }
    [HttpGet("logout")]
    public IActionResult Logout()
    {
      var cookies = this.HttpContext.Response.Cookies;
      cookies.Delete("userid");
      cookies.Delete("username");

      return Redirect(@"/");
    }
  }
}
